package com.minhua.jijiehao2.utils;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static com.baidu.ocr.sdk.utils.ImageUtil.calculateInSampleSize;

/**
 * @author moo
 * @date 2018/1/4.
 * @describe
 */

public class BitmapUtils {
    /**
     * 质量压缩图片
     */
    public static Bitmap getBitmap(String imgPath, int maxWidth,
                                   int maxHeight, boolean isDeleteFile) {
        File file = new File(imgPath);
        if (file.exists()) {
            return getBitmap(file, maxWidth, maxHeight, isDeleteFile);
        } else {
            return null;
        }
    }

    /**
     * 质量压缩图片
     */
    public static Bitmap getBitmap(File file, int maxWidth,
                                   int maxHeight, boolean isDeleteFile) {
        Bitmap bitmap;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        options.inJustDecodeBounds = false;
        options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
        if (file.exists()) {
            bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            if (isDeleteFile) {
                file.delete();
            }
        }
        return bitmap;
    }


    /**
     * MIUI系统的相册选择
     */
    public static Bitmap setPhotoForMiuiSystem(Context context, Intent data, int maxWidth,
                                               int maxHeight) {
        Uri localUri = data.getData();
        String scheme = localUri.getScheme();
        String imagePath = "";
        if ("content".equals(scheme)) {
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = context.getContentResolver().query(localUri, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            imagePath = c.getString(columnIndex);
            c.close();
        } else if ("file".equals(scheme)) {
            //小米4选择云相册中的图片是根据此方法获得路径
            imagePath = localUri.getPath();
        }
        return getBitmap(imagePath, maxWidth, maxHeight, false);
    }

    public static Bitmap getPhotoForNormalSystem(Context context, Intent data, int maxWidth,
                                                 int maxHeight) {
        Uri selectedImage = data.getData();
        String[] filePathColumns = new String[]{MediaStore.Images.Media.DATA};
        Cursor c = context.getContentResolver().query(selectedImage, filePathColumns, null, null, null);
        c.moveToFirst();
        int columnIndex = c.getColumnIndex(filePathColumns[0]);
        String imagePath = c.getString(columnIndex);
        c.close();
        return getBitmap(imagePath, maxWidth, maxHeight, false);
    }


    /**
     * bitmap  ->  file
     */
    public static File bitmapToFile(Bitmap bm) {
        if (FileUtils.getRootCatchFile() == null) {
            return null;
        }
        String fileName = FileUtils.getRootCatchFile().getAbsolutePath()+"/img/" + System.currentTimeMillis() + ".png";
        File myCaptureFile = new File(fileName);
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
            bm.compress(Bitmap.CompressFormat.PNG, 90, bos);
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return myCaptureFile;
    }
}
